package com.xiaocz.hblsubstitutedriving.wxapi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.xiaocz.common.utils.ImgUtil;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.model.PayReqEntity;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 微信分享
 */
public class Wechat {


//    /**
//     * 分享图片
//     * @param bitmap
//     * @param shareType
//     */
//    public static void sharePicture(Bitmap bitmap, int shareType) {
//        WXImageObject imgObj = new WXImageObject(bitmap);
//        WXMediaMessage msg = new WXMediaMessage();
//        msg.mediaObject = imgObj;
//        bitmap.recycle();
//        SendMessageToWX.Req req = new SendMessageToWX.Req();
//        req.transaction = buildTransaction("imgshareappdata");
//        req.message = msg;
//        req.scene = shareType;
//        App.mWxApi.sendReq(req);
//    }

    /**
     * @param context     上下文
     * @param title       微信分享标题
     * @param description 微信分享详情
     * @param url         微信分享链接
     * @param bmp     微信图片
     * @param shareType   0聊天界面，1朋友圈
     */
    public static void share(Context context, final String title, final String description, final String url,
                             Bitmap bmp, int shareType) {
        if (!App.mWxApi.isWXAppInstalled()) {
            App.getInstance().showMessage("您还未安装微信客户端");
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        if(bmp==null){
            //写的固定图片，微信不支持图片链接(img_url)
            bmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
        }
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
        msg.thumbData = ImgUtil.bmpToByteArray(thumbBmp, true);
        thumbBmp.recycle();
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        switch (shareType) {
            case 0://发送到聊天界面
                msg.title = title;
                msg.description = description;
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneSession;
                App.mWxApi.sendReq(req);
                break;
            case 1://发送到朋友圈
                msg.title = title;
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
                App.mWxApi.sendReq(req);
                break;
        }
    }
    public static Bitmap getBitmap(final String path) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    if(conn.getResponseCode() == 200){
                        InputStream inputStream = conn.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

        return null;
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    public static void pay(PayReqEntity payReqEntity) {
        PayReq request = new PayReq();
        request.appId = payReqEntity.getAppid();
        request.partnerId = payReqEntity.getPartnerid();
        request.prepayId = payReqEntity.getPrepayid();
        request.packageValue = payReqEntity.getPackageX();
        request.nonceStr = payReqEntity.getNoncestr();
        request.timeStamp = payReqEntity.getTimestamp();
        request.sign = payReqEntity.getSign();
        request.extData = "科技有限公司";
        App.mWxApi.sendReq(request);
    }

}
